//
//  RecordTableViewCell.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/21.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "RecordTableViewCell.h"
#import "ActionModel.h"

#import "ControlButton.h"

@interface RecordTableViewCell()

@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *nickname;
@property (weak, nonatomic) IBOutlet UIScrollView *tagContainerView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;


@end

@implementation RecordTableViewCell

- (void)setRm:(ResultModel *)rm{
    _nickname.text = rm.playerModel.playerName;
    _scoreLabel.text = NSStringFormat(@"%ld",rm.score);
    if ([rm.playerModel.playerId isEqual:_owner.playerId]) {
        _nickname.textColor = [UIColor redColor];
        _scoreLabel.textColor = [UIColor redColor];
    }else{
        _nickname.textColor = [UIColor blackColor];
        _scoreLabel.textColor = [UIColor blackColor];
    }
    
    NSMutableArray *attrDisplayArr = [NSMutableArray new];
    NSMutableArray *attrDisplayActionId = [NSMutableArray new];
    for (ActionModel *am in _gameModel.actionArr) {
        if ([am.owner.playerId isEqualToString:rm.playerModel.playerId]) {
            [attrDisplayArr addObject:am.displayName];
            [attrDisplayActionId addObject:am.actionId];
        }
    }
    
    for (UIView *v in _tagContainerView.subviews) {
        [v removeFromSuperview];
    }
    
    float x = 0;
    
    for (int i=0;i<attrDisplayArr.count;i++) {
        NSString *title = attrDisplayArr[i];
        NSArray *displayNames = [title componentsSeparatedByString:@" "];
        
        title = [title stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
        
        ControlButton *btn = [[ControlButton alloc]initWithTitle:title fontSize:24];
        
        if (displayNames.count > 4) {
            btn.titleLabel.font = DefaultSystemFont(18);
        }
        if (displayNames.count > 5) {
            btn.titleLabel.font = DefaultSystemFont(14);
        }
        if (displayNames.count > 9) {
            btn.titleLabel.font = DefaultSystemFont(10);
        }
        btn.layer.borderWidth = 1.f;
        btn.titleLabel.numberOfLines = 0;
        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
        CGRect frame = btn.frame;
        frame.size.width = frame.size.width + 20;
        frame.origin.y = 0;
        frame.origin.x = CGRectGetMaxX(_tagContainerView.subviews.lastObject.frame)+5;
        frame.size.height = self.frame.size.height - 16;
        btn.frame = frame;
        x+= 5 + frame.size.width;
        
        //标签点击事件:只能改自己的
        if ([rm.playerModel.playerId isEqual:_owner.playerId]) {
            
            btn.layer.borderColor = [UIColor redColor].CGColor;
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            
            [btn addTapBlock:^(UIButton *btn) {
                for (ActionModel *am in _gameModel.actionArr) {
                    if ([am.actionId isEqualToString:attrDisplayActionId[i]]) {
                        if (_tagClickBlock) {
                            _tagClickBlock(am);
                        }
                        break;
                    }
                }
            }];
            
        }
        
        [_tagContainerView addSubview:btn];
        [_tagContainerView setContentSize:CGSizeMake(x, 42)];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
